<?php


namespace app\mobile\controller;


use app\common\AES;
use app\middleware\Controller;
use app\model\Cart as SelfModel;
use app\model\GoodsInventory;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;
class Cart extends Base
{
    /**
     * 添加单一商品到购物车
     * goods_id
     * member_id
     * attr_values
     * number
     * type
     */
    public function create()
    {

        try {
            if ($this->param['type'] == 2) {    //立即购买 先删除会员的然后再添加
                SelfModel::where('member_id', $this->member_id)->where('type', 2)->delete();
            }
            $cart = SelfModel::where('goods_id', $this->param['goods_id'])->where('attr_values', $this->param['attr_values'])->where('type', $this->param['type'])->find();
            if (!empty($cart)) { //如果购物车里包含这个商品的属性
                $ret = (new SelfModel())->where('id', $cart->id)->inc('number', $this->param['number'])->update();
                if ($ret) {
                    return json(['code' => 200, 'message' => '商品数量添加成功', 'data' => $cart]);
                }
                return json(['code' => 400, 'message' => '失败']);
            } else {
                $ret = SelfModel::create($this->param);
                if ($ret) {
                    if ($this->param['type'] == 1) { //购物车
                        return json(['code' => 200, 'message' => '商品添加成功', 'data' => $ret]);
                    }
                    if ($this->param['type'] == 2) { //
                        return json(['code' => 200, 'message' => '去订单页面', 'data' => $ret]);
                    }

                }
                return json(['code' => 400, 'message' => '失败']);
            }
        }catch (DbException $e) {
            return json(['code' => 400, 'message' => $e->getMessage()]);
        }

    }

    /**
     * 购物车列表
     * 根据shop_id来区分购物车
     * member_id
     */
    public function index()
    {
        $dat = SelfModel::where('member_id', $this->member_id)->where('type',1)->select()->toArray();
        $data = [];
        $total_num = 0;
        $total_price = 0;
        foreach ($dat as $k => $v) {
            $data[$v['shop_id']]['shop_id'] = $v['shop_id'];
            $data[$v['shop_id']]['list'][] = $v;
            $total_num = bcadd($total_num, $v['number'], 0);
            $total_price += $v['price']*$v['number'];
        }
        return view('',['data'=>$data,'total_num'=>$total_num,'total_price'=> price($total_price)]);
    }

    /**
     * 删除购物车单一商品
     * 购物车id
     */
    public function destory()
    {
        $ret = SelfModel::destroy(function($query){
            $query->where('id','in',$this->param['id']);
        });
        if ($ret) {
            return json(['code' => 200, 'message' => '商品删除成功']);
        }
        return json(['code' => 400, 'message' => '失败']);
    }

    /**
     * 购物车checkbox状态
     */

    public function checkbox_tab()
    {
        $cart = SelfModel::where('id', $this->param['id'])->find();
        if ($cart['checkbox'] == 1) {
            $cart->save(['checkbox' => 2]);
            return json(['code'=>200,'message'=>'取消选中']);
        }
        if ($cart['checkbox'] == 2) {
            $cart->save(['checkbox' => 1]);
            return json(['code'=>200,'message'=>'选中成功']);
        }

    }
    /**
     * 清空购物车
     * member_id
     * type = 1
     */
    public function clear_cart()
    {
        $ret = SelfModel::where(['member_id' => $this->param['member_id'], 'type' => 1])->delete();
        if ($ret) {
            return json(['code' => 200, 'message' => '购物车清除成功']);
        }
        return json(['code' => 400, 'message' => '失败']);
    }

    /**
     * 商品属性列表
     * 用户修改购物车属性
     */
    public function goods_attr_list()
    {
        $data = GoodsInventory::where('goods_id', $this->param['goods_id'])->field('id,goods_id,attr_values,inventory')->select();
        return json(['code' => 200, 'message' => '属性列表', 'data' => $data]);
    }

    /**
     * 修改当天id购物车商品数量或者属性值
     * 商品数量加和减
     * id 购物车id
     * goods_id 商品id
     * attr_values 属性值
     * number 数量
     * shop_id = 0
     */
    public function update_attr_number()
    {

        if($this->param['number'] < 1){
            return json(['code' => 400, 'message' => '修改数量小于1']);
        }
        $model = new SelfModel;
        $cart = $model->where('id', $this->param['id'])->find();
        $inventory = (new GoodsInventory())->where('goods_id', $cart['goods_id'])->where('attr_values', $this->param['attr_values'])->value('inventory'); //属性库存数量
        if ($inventory < $this->param['number']) { //如果库存数量小于请求的数量
            return json(['code' => 400, 'message' => '库存数量不足,请小于: ' . $inventory,'data'=> $inventory]);//返回库存数量 用于前台修改
        }

        $dat = SelfModel::where('member_id', $this->param['member_id'])->where('type',1)->select()->toArray();
        $data = [];
        $total_num = 0;
        $total_price = 0;
        foreach ($dat as $k => $v){
            $total_num = bcadd($total_num, $v['number'], 0);
            $total_price += $v['price']*$v['number'];
        }

        $ret = $model->where('id', $this->param['id'])->update(['attr_values' => $this->param['attr_values'], 'number' => $this->param['number']]);
        if ($ret) {
            return json(['code' => 200, 'message' => '修改成功', 'data' => $ret,'total_price'=>$total_price,'total_num'=>$total_num]);
        }
        return json(['code' => 400, 'message' => '失败']);

    }

    /**
     * 订单列表
     */
    public function order_list()
    {
        $dat = SelfModel::where('id', 'in',$this->param['cart_id_str'])->select()->toArray();
        $data = [];
        $total_num = 0;
        $total_price = 0;
        foreach ($dat as $k => $v) {
            $data[$v['shop_id']]['shop_id'] = $v['shop_id'];
            $v['number_price'] = price($v['price'] * $v['number']);
            $data[$v['shop_id']]['list'][] = $v;
            $total_num = bcadd($total_num, $v['number'], 0);
            $total_price += $v['price'] * $v['number'];
        }
        $default_address = ['name'=>'','mobile'=>'','prov'=>'','city'=>'','area'=>'','address'=>''];

        $default_address = \app\model\Address::where('member_id',$this->member_id)->order(['status'=>'asc'])->find();

        if(!empty($default_address)){

            $default_address = $default_address;
        }else{
            $default_address = [];
        }

        if(!empty($this->param['address_id'])){

            $default_address = \app\model\Address::where('id',$this->param['address_id'])->order(['status'=>'asc'])->find();
        }
        $crypt_data = (new AES())->encrypt(['data' => $data, 'total_num' => $total_num, 'total_price' => price($total_price),'address' => $default_address]);

        return view('',['data' => $data, 'total_num' => $total_num, 'total_price' => price($total_price),'address' => $default_address,'crypt_data'=>$crypt_data]);
    }




}